package com.ansion.basedb.storage;

import android.content.SharedPreferences;

import androidx.annotation.NonNull;

import com.ansion.basedb.storage.api.MMKVStorage;
import com.ansion.basedb.storage.secure.CipherKeyManager;
import com.tencent.mmkv.MMKV;

/**
 * @author john on 2021/7/27.
 * @version 1.0
 * @desc
 */
class LiteStorageImpl implements MMKVStorage {
    final String prefix = "bot.litekvs.";
    MMKV mmkv;

    public LiteStorageImpl(String tableName, boolean encrypt) {
        String key = prefix + tableName;
        long current = System.currentTimeMillis();
        if (encrypt) {
            //v2秘钥获取方案改为使用mmkv存储
            key = key + "_enc";
            String cryptKey = CipherKeyManager.getInst().getPassword(tableName);
            current = System.currentTimeMillis();
            mmkv = MMKV.mmkvWithID(key, MMKV.SINGLE_PROCESS_MODE, cryptKey);
        } else {
            mmkv = MMKV.mmkvWithID(key, MMKV.SINGLE_PROCESS_MODE);
        }
        long cost = System.currentTimeMillis() - current;
        //下面的log会出这个问题: java.lang.NoSuchMethodError: No static method ѧǥ'so(J)Ljava/lang/Long; in class Ljava/lang/Long; or its super classes (declaration of 'java.lang.Long' appears in /apex/com.android.runtime/javalib/core-oj.jar)
        //VLog.i(LogTag.TAG_STORAGE, " init mmkv for %s,cost:%d", key, cost);
    }

    public LiteStorageImpl(String mmkvName, String password) {
        mmkv = MMKV.mmkvWithID(mmkvName, MMKV.SINGLE_PROCESS_MODE, password);
    }


    @Override
    public boolean remove(@NonNull String key) {
        mmkv.remove(key);
        return true;
    }

    @Override
    public boolean contains(@NonNull String key) {
        return mmkv.containsKey(key);

    }

    @Override
    public boolean putInt(@NonNull String key, int value) {
        return mmkv.encode(key, value);
    }

    @Override
    public int getInt(@NonNull String key, int defValue) {
        return mmkv.decodeInt(key, defValue);
    }

    @Override
    public boolean putLong(@NonNull String key, long value) {
        return mmkv.encode(key, value);
    }

    @Override
    public long getLong(@NonNull String key, long defValue) {
        return mmkv.decodeLong(key, defValue);
    }

    @Override
    public boolean putFloat(@NonNull String key, float value) {
        return mmkv.encode(key, value);

    }

    @Override
    public float getFloat(@NonNull String key, float defValue) {
        return mmkv.decodeFloat(key, defValue);
    }

    @Override
    public boolean putString(@NonNull String key, String value) {
        return mmkv.encode(key, value);

    }

    @Override
    public String getString(@NonNull String key, String defValue) {
        return mmkv.decodeString(key, defValue);
    }

    @Override
    public boolean putBoolean(@NonNull String key, boolean value) {
        return mmkv.encode(key, value);
    }

    @Override
    public boolean getBoolean(@NonNull String key, boolean defValue) {
        return mmkv.decodeBool(key, defValue);
    }

    @Override
    public boolean putByteArray(@NonNull String key, byte[] value) {
        return mmkv.encode(key, value);

    }

    @Override
    public byte[] getByteArray(@NonNull String key, byte[] defValue) {
        return mmkv.decodeBytes(key, defValue);
    }

    @Override
    public void clearAll() {
        mmkv.clear();
    }

    @Override
    public int importFromSharedPreferences(SharedPreferences sp) {
        return mmkv.importFromSharedPreferences(sp);
    }

    @Override
    public SharedPreferences sharePreference() {
        return mmkv;
    }

    @Override
    public SharedPreferences.Editor getEditor() {
        return mmkv.edit();
    }

    @Override
    public String[] allKeys() {
        return mmkv.allKeys();
    }
}
